<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;
use app\admin\model\Articles;
use app\admin\controller\blog\Sorts;

//use app\common\controller\Backend;
class Category extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        $url = $this->request->request('url');
        $sortid = $this->request->request('sortid')?$this->request->request('sortid'):"";
        $this->comments = new \app\admin\model\Comments;
        $this->articles = new \app\admin\model\Articles;
        $this->sorts = new \app\admin\model\Sorts;
        //文章列表
        $articleslist = $this->articles
            ->with(['blogsorts', 'bloglabels'])
            ->where(['status'=> 1,'sorts_ids'=>$sortid])
            ->order('updatetime desc')
            // ->order($sort, $order)
            // ->limit($offset, $limit)
            ->paginate(10,'',['query'=>['sortid'=>$sortid]]);//分页传入分类ID
        foreach ($articleslist as $row) {
            $totalcomments = $this->comments
                ->where('article_id', '=', $row['id'])
                ->count();
            $row->totalcomments = $totalcomments;
            $row->article_content = strip_tags($row['article_content']);
        }
        // 获取分页显示
        //$page = $articleslist->render();
        //文章列表end
        //热门列表
       $hotlist = $this->articles->hotarticlelist();
        //分类列表
        $topsortslist = $this->sorts->gettopsortslist();
        $getnowdate=new \app\index\controller\Common();
        $category=$this->sorts->where('id',$sortid)->find();
        // 模板变量赋值
        $this->assign('articleslist', $articleslist);
        //$this->assign('page', $page);
        $this->assign('hotlist', $hotlist);
        $this->assign('topsortslist', $topsortslist);
        $this->assign('category', $category);
        
        $this->assign('nowdate', $getnowdate->getnowdate());

        // 渲染模板输出
        return $this->view->fetch();
    }

}
